package com.jrx.anytxn.reporting.job.customer.config;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.customer.entity.CmCustomerSecondLevel;
import com.jrx.anytxn.param.entity.BtBatchStatus;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtBtBatchStatusMapper;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.bean.CustomerDataBean;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.customer.step.AutoAssemCustomerProcessor;
import com.jrx.anytxn.reporting.job.customer.step.AutoAssemCustomerReader;
import com.jrx.anytxn.reporting.job.customer.step.AutoAssemCustomerWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * @author ：lihm
 * @date ：2019/11/26 21:53
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class AutoAssemCustomerConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(AutoAssemCustomerConfiguration.class);


    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private AutoAssemCustomerProcessor autoAssemCustomerProcessor;

    @Autowired
    private AutoAssemCustomerWriter autoAssemCustomerWriter;

    @Resource
    private ExtBtBatchStatusMapper extBtBatchStatusMapper;

    @Bean
    public Job autoAssemCustomerJob(){
        return jobBuilderFactory.get("autoAssemCustomerJob")
                .start(autoAssemCustomerJobStep())
                .build();
    }

    @Bean
    public Step autoAssemCustomerJobStep(){
        return stepBuilderFactory.get("autoAssemCustomerJobStep")
                .<CmCustomerSecondLevel, CustomerDataBean>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(autoAssemCustomerReader())
                .processor(autoAssemCustomerProcessor)
                .writer(autoAssemCustomerWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<CmCustomerSecondLevel> autoAssemCustomerReader(){
        logger.info("抽取客户信息批量启动,机构号：{}",ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);

        BtBatchStatus statuses = extBtBatchStatusMapper.selectByStatusAndJobName(ReportingConstant.BATCH_SATTUS_2,
                ReportingConstant.CUTOVER_END,org.getTenantId(),org.getToday());
        String startTime = null;
        if(null != statuses){
            startTime = DateUtils.format(statuses.getCreateTime(),"yyyy-MM-dd HH:mm:ss");
        }

        return new AutoAssemCustomerReader(sqlSessionFactory,anyTxnBatchProperties.getReportingCustomerConfig().getPageSize(),startTime);
    }

}
